package lzk.myshop.service.impl;

import lzk.myshop.dao.*;
import lzk.myshop.Util.OrderToMer;
import lzk.myshop.pojo.Classify;
import lzk.myshop.pojo.Goods;
import lzk.myshop.pojo.Merchant;
import lzk.myshop.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    ClassifyMapper classifyMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public Map<String, Object> signIn(String account, String password) {
        Map<String,Object> result = new HashMap<>();
        Merchant merchant;
        if (account.contains("@")){
            merchant = merchantMapper.loginByEmail(account,password);
        }else {
            merchant = merchantMapper.loginByPhone(account,password);
        }
        if (merchant!=null){
            result.put("code",1);
            result.put("message","登陆成功");
            result.put("data",merchant);
        }else {
            result.put("code",0);
            result.put("message","登陆失败，账号或密码错误");
            result.put("data",null);
        }
        return result;
    }

    @Override
    public Map<String, Object> goodsOfMer(int merchantId) {
        List<Goods> l = goodsMapper.goodsOfMer(merchantId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",1);
        result.put("message","查询到"+l.size()+"条数据");
        result.put("date",l);
        return result;
    }

    @Override
    public Map<String, Object> addGoods(Goods goods) {
        int i = goodsMapper.addGoods(goods);
        Map<String,Object> result = new HashMap<>();
        if (i==1){
            result.put("code",1);
            result.put("message","添加成功");
            result.put("date",null);
        }else {
            result.put("code",0);
            result.put("message","添加失败");
            result.put("date",null);
        }
        return result;
    }

    @Override
    public Map<String, Object> allClassify(int merchantid) {
        Map<String,Object> result = new HashMap<>();
        List<Classify> l = classifyMapper.allClassify(merchantid);
        result.put("code",1);
        result.put("message","查找到"+l.size()+"条数据");
        result.put("date",l);
        return result;
    }

    @Override
    public Map<String, Object> alterGoods(Goods goods) {
        int i = goodsMapper.alterGoods(goods);
        Map<String,Object> result = new HashMap<>();
        if (i==1){
            result.put("code",1);
            result.put("message","修改成功");
            result.put("date",null);
        }else {
            result.put("code",-1);
            result.put("message","修改失败");
            result.put("date",null);
        }
        return result;
    }

    @Override
    public Map<String, Object> downGoods(int id) {
        int i = goodsMapper.downGoods(id);
        Map<String,Object> result = new HashMap<>();
        if (i==1){
            result.put("code",1);
            result.put("message","修改成功");
            result.put("date",null);
        }else {
            result.put("code",-1);
            result.put("message","修改失败");
            result.put("date",null);
        }
        return result;
    }

    @Override
    public Map<String, Object> addClassify(Classify classify) {
        int i = classifyMapper.addClassify(classify);
        Map<String,Object> result = new HashMap<>();
        if (i==1){
            result.put("code",1);
            result.put("message","添加成功");
            result.put("date",null);
        }else {
            result.put("code",-1);
            result.put("message","添加失败");
            result.put("date",null);
        }
        return result;
    }


    @Override
    public Map<String, Object> deleteClassify(int id) {
        int i=classifyMapper.deleteClassify(id);
        Map<String,Object> result = new HashMap<>();
        if (i==1){
            result.put("code",1);
            result.put("message","删除成功");
            result.put("date",null);
        }else {
            result.put("code",-1);
            result.put("message","删除失败");
            result.put("date",null);
        }
        return result;
    }

    @Override
    public Map<String, Object> getAllMerOrders(int merid) {
        Map<String,Object> result = new HashMap<>();
        List<OrderToMer> orderToMers = orderMapper.getAllMerOrder(merid);
        result.put("code",1);
        result.put("message","查询到"+orderToMers.size()+"条记录");
        result.put("data",orderToMers);
        return null;
    }

    @Override
    public Map<String, Object> sentGoods(int id) {
        int i = orderMapper.sentGoods(id);
        Map<String,Object> result = new HashMap<>();
        if (i==1){
            result.put("code",1);
            result.put("message","发货成功");
            result.put("date",null);
        }else {
            result.put("code",-1);
            result.put("message","发货失败");
            result.put("date",null);
        }
        return result;
    }

    @Override
    public Map<String, Object> create(Merchant merchant) {
        int a = merchantMapper.ifExist(merchant);
        Map<String,Object> result = new HashMap<>();
        if (a != 0){
            result.put("code",-1);
            result.put("message","邮箱或手机号已经存在");
            result.put("data",null);
        }else {
            int i = merchantMapper.create(merchant);
            if (i ==1){
                result.put("code",1);
                result.put("message","创建成功");
                result.put("data",null);
            }else {
                result.put("code",-2);
                result.put("message","创建失败");
                result.put("data",null);
            }
        }

        return result;
    }

    @Override
    public Map<String, Object> deleteMer(int MerID) {
        List<Integer> list = merchantMapper.allOrderOfMer(MerID);
        Map<String,Object> result = new HashMap<>();
        if (list.size()==0){
            int a = merchantMapper.deleteMer(MerID);
            if (a == 1){
                result.put("code",1);
                result.put("message","注销成功");
                result.put("data",null);
            }else {
                result.put("code",-1);
                result.put("message","注销失败");
                result.put("data",null);
            }
        }else {
            int count =0;
            for (int i=0;i< list.size();i++){
                count+=merchantMapper.ifCanDelete(list.get(i));
            }
            if (count == 0){
                result.put("code",1);
                result.put("message","注销成功");
                result.put("data",null);
            }else {
                result.put("code",-2);
                result.put("message","您还有订单在进行中，暂不能注销");
                result.put("data",null);
            }
        }
        return result;
    }
}
